package com.gsc.controller;

import com.gsc.constants.SystemConstants;
import com.gsc.domain.ResponseResult;
import com.gsc.domain.entity.LoginUser;
import com.gsc.domain.entity.User;
import com.gsc.domain.vo.AdminUserInfoVo;
import com.gsc.domain.vo.MenuVo;
import com.gsc.domain.vo.RoutersVo;
import com.gsc.domain.vo.UserInfoVo;
import com.gsc.enums.AppHttpCodeEnum;
import com.gsc.exception.SystemException;
import com.gsc.service.AdminLoginService;
import com.gsc.service.BlogLoginService;
import com.gsc.service.MenuService;
import com.gsc.service.RoleService;
import com.gsc.utils.BeanCopyUtils;
import com.gsc.utils.RedisCache;
import com.gsc.utils.SecurityUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * @author gscstart
 * @create 2023/2/2-16:27
 */
@RestController
public class AdminLoginController {

    @Autowired
    private AdminLoginService adminLoginService;
    @Autowired
    private MenuService menuService;
    @Autowired
    private RoleService roleService;
    @Autowired
    private RedisCache redisCache;

    /**
     * @RequestBody：告诉springmvc在请求体中获取参数
     * 后台用户登录接口
     * @param user
     * @return
     */

    @PostMapping("/user/login")
    public ResponseResult login(@RequestBody User user){
        //前端必须传用户名过来，在这做个判断
        if(!StringUtils.hasText(user.getUserName())){
            //提示 必须要传用户名
//            throw new RuntimeException() 出现一个异常就throw，很麻烦 所以要做异常统一处理
            throw new SystemException(AppHttpCodeEnum.REQUIRE_USERNAME);
        }
        return adminLoginService.login(user);
    }

    /**
     * 退出登录的接口
     * @return
     */
    @PostMapping("/user/logout")
    public ResponseResult logout(){

        return adminLoginService.logout();
    }


    /**
     * 获取用户信息的接口
     * @return
     */
    @GetMapping("/getInfo")
    public ResponseResult<AdminUserInfoVo> getInfo(){
        //获取当前登录的用户->从这里SecurityContextHolder
        LoginUser loginUser = SecurityUtils.getLoginUser();
        //根据用户id查询权限信息
        List<String> perms = menuService.selectPermsByUserId(loginUser.getUser().getId());
        //根据用户id查询角色信息
        List<String> roleKeyList = roleService.selectRoleKeyByUserId(loginUser.getUser().getId());

        //获取用户信息
        User user = loginUser.getUser();
        UserInfoVo userInfoVo = BeanCopyUtils.copyBean(user, UserInfoVo.class);
        //封装数据返回
        AdminUserInfoVo adminUserInfoVo = new AdminUserInfoVo(perms,roleKeyList,userInfoVo);
        return ResponseResult.okResult(adminUserInfoVo);
    }

    /**
     * 动态路由接口
     */
    @GetMapping("/getRouters")
    public ResponseResult<RoutersVo> getRouters(){
        Long userId = SecurityUtils.getUserId();
        //查询menu  结果是tree形式
        List<MenuVo> menuVos = menuService.selectRouterMenuTreeByUserId(userId);
        //封装数据返回
        return ResponseResult.okResult(new RoutersVo(menuVos));
    }


}
